package Servlet;

import com.mysql.jdbc.StringUtils;
import pojo.Flight;
import service.FlightService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/selectFlightServlet")
public class SelectFlightServlet extends HttpServlet {

   private FlightService flightService = new FlightService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.  解决乱码：
        request.setCharacterEncoding("UTF-8");

        List<Flight> allFlights = new ArrayList<>();

        String addFlag = (String) request.getAttribute("addFlag");

        String destination = request.getParameter("destination");

        String flyTime = request.getParameter("flyTime");

        //2. 调用service查询
        if (!StringUtils.isNullOrEmpty(addFlag)  && !StringUtils.isNullOrEmpty(destination)) {
            allFlights = flightService.selectByDestination(destination);}
         else if (flyTime != null) {
            allFlights = flightService.selectByFlyTime(flyTime);
        } else {
            allFlights = flightService.selectAllFlights();
        }
        //3. 存储到request中
        request.setAttribute("allFlights", allFlights);
        request.getRequestDispatcher("/flyList.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}